home *** CD-ROM | disk | FTP | other *** search
/ Total Web Page (Professional Suite) / Total Web Page 99.iso / CGI / SSIS.HTML < prev    next >
Text File  |  1996-06-10  |  7KB  |  260 lines

  1. #! /usr/bin/perl
  2. #
  3. # Server-Side Include Substitute (ssis)
  4. #
  5. # Version 1.1.3 - August 1, 1995 - Removed the need for cgi-lib.pl
  6. # Version 1.1.2 - May 9, 1995 - Slight documentation chages - GEB
  7. # Version 1.1.1 - Mar 10, 1995 - modified it to work if . isn't in path - GNS
  8. # Version 1.1 - Mar 3, 1995
  9. # Version 1.0 - Mar 1, 1995
  10. #
  11. # This script is meant to allow server-side include functionality without
  12. # *actually* using server-side includes.  It does this by running as a
  13. # CGI program, and parsing the pages itself.
  14. #
  15. # Written by George Burgyan <gburgyan@nforce.com> and Gabe Schaffer
  16. # <gabe@gabe.com>
  17. #
  18. ###########################################################################
  19. #
  20. # Usage:
  21. #
  22. #  Put this program in the directory where you put your CGI programs.
  23. #  Call the program like this:
  24. #
  25. #   http://www.site.edu/cgi-bin/your_dir/ssis/~username/the_page_you_want.html
  26. #
  27. #  This will send the document
  28. #
  29. #   http://www.site.edu/~username/the_page_you_want.html
  30. #
  31. #  except it will handle the server-side includes.
  32. #
  33. #  If you're using the CERN server, you can have the server map the URL
  34. #  into the proper format by putting this in your httpd.conf file:
  35. #
  36. #    Map /dir/filename.html /cgi-bin/ssis/dir/filename.html
  37. #
  38. #  which will map any requests transparently onto the ssis script URL.
  39. #
  40. ###########################################################################
  41. #
  42. # Bugs:
  43. #
  44. #  Right now it will only handle the <!--#exec cgi="program"--> server-
  45. #  side includes.  This limitation will be removed shortly. 
  46. #
  47. ###########################################################################
  48. # Leave the next line alone...  it's used in the default config.
  49. #chop($pwd = `/bin/pwd`);    # save this for later
  50. ###########################################################################
  51. #
  52. #   Configuration section
  53. #
  54. ###########################################################################
  55. #
  56. # $ScriptAlias
  57. #
  58. # The directory to look in to find CGI programs.  Normally it is something
  59. # like '/usr/local/etc/httpd/cgi-bin' or something like it.  On CERN systems
  60. # it may be called something like 'htbin' or something.
  61. #
  62. # The following GUESSES where the cgi-bin directory is on the assumption
  63. # that this script is called with an absolute path.
  64. # If this does not work, you have to actually set it.
  65. # I have tested it on my configuration with both NCSA and CERN httpd and
  66. # it worked on those.  Your mileage may vary.
  67. ($ScriptAlias{"/cgi-bin/"}) = ($0 =~ m!^(.*/)?.*$!);
  68. #
  69. #$ScriptAlias{"/cgi-bin/"} = "/usr/httpd/cgi-bin/";
  70. #
  71. # Also note that you can set up *multiple* aliases.  (this is not that
  72. # often used.)
  73. #%ScriptAlias = ( "/cgi-bin/", "/usr/cgi-bin/",
  74. #          "/htbin/", "/usr/cgi-bin",
  75. #        );
  76. #
  77. ###########################################################################
  78. #
  79. # $DocumentRoot
  80. #
  81. # Set this variable to point at the *systems* home web directory.  It might
  82. # be called '/usr/local/etc/httpd/htdocs' or something.
  83. #
  84. #$DocumentRoot = "/usr/webdocs";
  85. #
  86. # If you are running a multi-homed host (ie. one machine looks like many
  87. # different web servers) you can set this up for each server so it looks
  88. # in a different directory based on which "server" was accessed.
  89. #
  90. #%DocumentRoot = ( "www.cybercon.com", "/usr/webdocs",
  91. #                  "www.gabe.com", "/home/gabe/public_html",
  92. #                );
  93. #
  94. ###########################################################################
  95. #
  96. # $DirectoryIndex
  97. #
  98. # This should be the default file returned if a directory is requested.
  99. #
  100. $DirectoryIndex = "index.html";
  101. #
  102. ###########################################################################
  103. #
  104. # This is set to be the user's web directory. (ie. where you put your web
  105. # documents.  Default NCSA is public_html.
  106. #
  107. $UserDir = "public_html";
  108. #
  109. ###########################################################################
  110. #
  111. # End of configuration info
  112. #
  113.  
  114. #print "<pre>". `env` . `/bin/pwd`."</pre>\n";
  115.  
  116. if (!$ENV{"PATH_TRANSLATED"}) 
  117. {
  118.     print "Content-type: text/html\n\n";
  119.     &ServerError;
  120. }
  121.  
  122. $file = $ENV{"PATH_TRANSLATED"};
  123.  
  124. if (-d $file && !($file =~ /\/$/)) { # If it points to a directory without a
  125.                 # '/' after it, relative links will be broken,
  126.                 # send a redirect to the client to make
  127.                 # the client look in the right place.
  128.     print "Location: http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}$ENV{SCRIPT_NAME}$ENV{PATH_INFO}/\n\n";
  129.     exit;
  130. }
  131.  
  132. $ENV{"DOCUMENT_URI"} = $ENV{"PATH_INFO"};
  133.  
  134. if (-d $file) {
  135.     $file .= $DirectoryIndex;
  136.     $ENV{"DOCUMENT_URI"} .= $DirectoryIndex;
  137. }
  138.  
  139. if (!($file =~ m!^.*\.html?!)) # is it an html (or htm)?
  140. {                # it's not, so we can't parse it
  141.     print "Location: $ENV{PATH_INFO}\n\n"; # have the server send it out raw
  142.     exit;
  143. }
  144.  
  145. print "Content-type: text/html\n\n";
  146.  
  147. open(HTML,$file) || &ServerError;
  148.  
  149. $/ = ">";            # set paragraph separator
  150. #@tags = split("<",$entire);
  151. #foreach $line (@tags)
  152. while ($line = <HTML>)
  153. {
  154.     $/ = "\n";            # gotta put it back the way it was
  155.  
  156.     # Look through the file looking for a server-side include...
  157.     if ($line =~ /<!--#exec\s+cgi\s*=\s*"([^"]+)"\s*-->/)
  158.     {
  159.     $file = $1;
  160.     print $`;
  161.     $end = $';
  162.  
  163.     # Pick the right ScriptAlias from the list.
  164.     foreach $a (keys %ScriptAlias) 
  165.     {
  166.         last if $file =~ s!^$a!$ScriptAlias{$a}!;
  167.     }
  168.  
  169.     undef $path_info;
  170.  
  171.     # Find the actual program and the residual PATH_INFO
  172.     while ((!-f $file || !-x $file) && length($file))
  173.     {
  174.         $pos = rindex($file, "/");
  175.         $path_info = substr($file, $pos) . $path_info;
  176.         $file = substr($file, 0, $pos);
  177.     }
  178.  
  179.     ($dir, $prog) = ($file =~ m!^(.*)/(.*)$!);
  180.     chdir $dir;
  181.     
  182.     if (defined $path_info)
  183.     {
  184.         $ENV{"PATH_INFO"} = $path_info;
  185.         ($uid, $path) = ($path_info =~ m!/~([^/])(.*)!);
  186.  
  187.         if ($uid)
  188.         {
  189.         $path_tran = (getpwnam($uid))[7] . $path_info . "/" . $UserDir;
  190.         }
  191.         else
  192.         {
  193.         $path_tran = $DocumentRoot . $path_info;
  194.         }
  195.     }
  196.  
  197.     # Make sure that the program that we are about to call is in fact
  198.     # an executable program...  If it isn't, spit out something to
  199.     # that effect.
  200.  
  201.     if (! -e $prog) 
  202.     {
  203.         print "[ssis: program not found. " .
  204. "<a href=\"http://www.webtools.org/ssis/noprog.html\"" .
  205. ">More info availible</a>]";
  206.     } 
  207.     elsif (! -x $prog) 
  208.     {
  209.         print "[ssis: non executable program specified: '$dir' / '$prog'" .
  210. " <a href=\"http://www.webtools.org/ssis/badprog.html\">" .
  211. "More info availible</a>]";
  212.     } 
  213.     else 
  214.     {
  215.         # We have a program that exists and that we can run.  Run it!
  216.         open (INCLUDE, "./$prog|");
  217.  
  218.         $gotblank = 0;
  219.         while ($rline = <INCLUDE>) 
  220.         {
  221.         print $rline if $gotblank;
  222.         $gotblank = 1 if $rline eq "\n";
  223.         }
  224.         
  225.         close (INCLUDE);
  226.     }
  227.     
  228.     print $end;
  229.     } 
  230.     else 
  231.     {
  232.     print $line;
  233.     }
  234.     $/ = ">";            # set paragraph separator
  235. }
  236.  
  237.  
  238. # This spits out a rather official-looking error message along with any
  239. # info that we were passed.
  240.  
  241. sub ServerError {
  242.     local($message) = @_;
  243.  
  244.     print <<EOM;
  245. <html><head><h1>500 Server Error</h1></head><body>
  246.  
  247. The ssis program was called in such a way that it was unable
  248. to complete your request.<p>
  249.  
  250. Please contact the server administrator, and inform them of the time the error
  251. occurred, and anything you might have done that may have caused the error.<p>
  252.  
  253. $message
  254. </body></html>
  255. EOM
  256.     exit(1);
  257. }
  258.